//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Plain.Merge
{
	public abstract class Merger
	{
		protected IDictionary<String, String> fields = new Dictionary<String, String>();
		protected IDictionary<String, String> domainFields = new Dictionary<String, String>();
		//protected DomainUIMapper uiMapper;
		//protected String seperator = String.Empty;

		//public Merger(String mapper, String seperator, ApplicationUser user)
		//{
		//   uiMapper = (DomainUIMapper)ConfigurationManager.GetSection(mapper);
		//   this.seperator = seperator;
		//   mapEntity(user, "Sagsbehandler", typeof(ApplicationUser));
		//}
		//public Merger(String mapper, String seperator, ApplicationUser user, BusinessEntity entity)
		//{
		//   uiMapper = (DomainUIMapper)ConfigurationManager.GetSection(mapper);
		//   this.seperator = seperator;
		//   mapEntity(user, "Sagsbehandler", typeof(ApplicationUser));
		//   mapEntity(entity);
		//}
		//public Merger(String mapper, String seperator, ApplicationUser user, BusinessEntity entity, string prefix)
		//{
		//   uiMapper = (DomainUIMapper)ConfigurationManager.GetSection(mapper);
		//   this.seperator = seperator;
		//   mapEntity(user, "Sagsbehandler", typeof(ApplicationUser));
		//   mapEntity(entity, prefix);
		//}

		public void AddValue(string key, string value)
		{
			if (fields.ContainsKey(key))
			{
				fields[key] = value;
			}
			else
			{
				fields.Add(key, value);
			}
		}
		/// <summary>
		/// Gets a list of fields mapped
		/// </summary>
		public IList<string> Fields
		{
			get
			{
				List<string> l = new List<string>();
				l.AddRange(fields.Keys);
				return l;
			}
		}

		//protected void mapEntity(BusinessEntity entity)
		//{
		//   mapEntity(entity, "");
		//}
		//protected void mapEntity(BusinessEntity entity, string prefix)
		//{
		//   if (entity != null)
		//   {
		//      mapEntity(entity, prefix, entity.GetType());
		//   }
		//}

		/// <summary>
		/// Maps a objects properties to real text
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="prefix"></param>
		/// <param name="type"></param>
		//protected virtual void mapEntity(BusinessEntity entity, string prefix, Type type)
		//{

		//   if (entity != null)
		//   {
		//      if (!String.IsNullOrEmpty(seperator) && prefix != null && prefix.Length > 0 && !prefix.EndsWith(seperator))
		//      {
		//         prefix += seperator;
		//      }
		//      else if (prefix == null)
		//      {
		//         prefix = String.Empty;
		//      }

		//      IDictionary<string, string> map = uiMapper.GetMapping(type);
		//      foreach (PropertyInfo prop in type.GetProperties())
		//      {
		//         if (map.ContainsKey(prop.Name))
		//         {
		//            if (!fields.ContainsKey(prefix + map[prop.Name]))
		//            {
		//               object o = prop.GetValue(entity, null);
		//               if (o != null)
		//               {
		//                  if (o is Boolean)
		//                  {
		//                     fields.Add(prefix + map[prop.Name], ((((Boolean)o) == true) ? "Ja" : "Nej"));
		//                  }
		//                  else if (o is DateTime)
		//                  {
		//                     fields.Add(prefix + map[prop.Name], ((DateTime)o).ToString("dd/MM/yyyy"));
		//                  }
		//                  else
		//                  {
		//                     fields.Add(prefix + map[prop.Name], o.ToString());
		//                  }
		//               }
		//               else
		//               {
		//                  fields.Add(prefix + map[prop.Name], String.Empty);
		//               }
		//            }
		//         }
		//         if (!domainFields.ContainsKey(prefix + prop.Name))
		//         {
		//            object o = prop.GetValue(entity, null);
		//            if (o != null)
		//            {
		//               if (o is Boolean)
		//               {
		//                  domainFields.Add(prefix + prop.Name, ((((Boolean)o) == true) ? "Ja" : "Nej"));
		//               }
		//               else if (o is DateTime)
		//               {
		//                  domainFields.Add(prefix + prop.Name, ((DateTime)o).ToString("dd/MM/yyyy"));
		//               }
		//               else
		//               {
		//                  domainFields.Add(prefix + prop.Name, o.ToString());
		//               }
		//            }
		//            else
		//            {
		//               domainFields.Add(prefix + prop.Name, String.Empty);
		//            }
		//         }
		//      }

		//   }
		//}
	}
}